package guoanmaker.personal.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.modelnobind.Coupons;
import guoanmaker.personal.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.personal.business.service.OperatorTimeService;
import guoanmaker.personal.business.service.PersonalOrderFormService;
import guoanmaker.personal.business.service.PersonalUserCouponService;
import guoanmaker.personal.helper.common.AjaxJson;
import guoanmaker.personal.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;

/**
 * 
* @ClassName: PersonalOrderFormController 
* @Description: 订单controller
* @author Yvonne_MGL
* @date 2017年7月12日 下午2:03:30 
*  
 */
@RestController
@RequestMapping(ApiController.ORDER_FORM_URL)
public class PersonalOrderFormController {
	
	@Resource
	private PersonalOrderFormService personalOrderFormService;
	
	@Resource
	private PersonalUserCouponService personalUserCouponService;
	
	@Resource
	private AjaxJson ajaxJson;
	
	@Resource
	private OperatorTimeService operatorTimeService;
	
	@Autowired  
	private  HttpServletRequest request;
	
	
	/**
	 * 
	* @Description: 创建一口价商品订单 
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:09:09     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建一口价商品订单", notes = "创建一口价商品订单")
	@RequestMapping(value = "/createNotBargainingOrder", method = RequestMethod.POST)
	public AjaxJson createNotBargainingOrder(String userid,String commodityid,String contactPerson,String contactNumber,String requirementDescription,String serverPlace,String rangeid){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createNotBargainingOrder(userid, commodityid, contactPerson, contactNumber, requirementDescription,"1",serverPlace,rangeid);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建议价商品订单 
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:14:23     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建议价商品订单", notes = "创建议价商品订单")
	@RequestMapping(value = "/createBargainingOrder", method = RequestMethod.POST)
	public AjaxJson createBargainingOrder(String userid,String commodityid,String contactPerson,String contactNumber,String requirementDescription,String serverPlace){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createBargainingOrder(userid, commodityid, contactPerson, contactNumber, requirementDescription,"1",serverPlace);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建工位租用订单
	* @param userid 用户id
	* @param commodityid 商品id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param days 租用天数
	* @param quantity 租用数量
	* @param remark 备注
	* @param placeOrderType 下单类型
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月23日 下午2:38:02     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建工位租用订单", notes = "创建工位租用订单")
	@RequestMapping(value = "/createSeatLeaseOrder", method = RequestMethod.POST)
	public AjaxJson createSeatLeaseOrder(String userid,String commodityid,String contactPerson,String contactNumber,String beginDate,String endDate,int quantity,String requirementDescription,String placeOrderType,String serverPlace){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createSeatLeaseOrder(userid, commodityid, contactPerson, contactNumber, beginDate,endDate, quantity, requirementDescription, "1",serverPlace);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 根据用户id和订单状态查询订单 
	* @param orderStatus 订单状态
	* @param userid 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:45:19     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询用户订单", notes = "查询用户订单")
	@RequestMapping(value = "/findByProperties", method = RequestMethod.POST)
	public AjaxJson findByProperties(String orderStatus, String userid) {
		List<ProvideOrderFormForOrder> orders=new ArrayList<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(orders);
		}else{
			try {
				orders=personalOrderFormService.findByProperties(orderStatus, userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(orders);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(orders);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 根据id获取订单详情 
	* @param id 订单id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 下午4:54:51     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询订单详情", notes = "查询订单详情")
	@RequestMapping(value = "/findByid", method = RequestMethod.POST)
	public AjaxJson findByid(String id){
		ProvideOrderFormForOrder order=new ProvideOrderFormForOrder();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(order);
		}else{
			try {
				order=personalOrderFormService.findByid(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(order);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(order);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 修改订单为完成支付状态
	* @param id 订单id
	* @param defrayNumber 支付单号
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月13日 上午10:15:38     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "完成付款", notes = "完成付款")
	@RequestMapping(value = "/overPay", method = RequestMethod.POST)
	public AjaxJson overPay(String id,String defrayNumber,String payType){
		ReturnResult returnResult=new ReturnResult();
		if (payType!=null && payType.equals("wxgzPAY")) {
			try {
				returnResult=personalOrderFormService.overPay(id, defrayNumber);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}else {
			String sessionid=request.getSession().getId();
			String sessionid1=request.getHeader("Authorization");
			if(sessionid1!=null){
				sessionid=sessionid1.substring(7,sessionid1.length());
			}
			Jedis jedis=new Jedis("123.56.48.63",6379);
			jedis.auth("218007");
			String value=jedis.get(sessionid);
			jedis.disconnect();
			if(value==null||!value.equals("1")){
				ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
			}else{
				try {
					returnResult=personalOrderFormService.overPay(id, defrayNumber);
					ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
				} catch (Exception e) {
					ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
				}
			}
		}

		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 确认完成订单 
	* @param id 订单id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月13日 上午11:21:07     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "确认完成订单", notes = "确认完成订单")
	@RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
	public AjaxJson confirmOrder(String id){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.confirmOrder(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 用户发起退款 
	* @param id 订单id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月13日 上午11:36:59     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "用户发起退款", notes = "用户发起退款")
	@RequestMapping(value = "/initiateRefunds", method = RequestMethod.POST)
	public AjaxJson initiateRefunds(String id,Double amount,String refundsReason){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.initiateRefunds(id,amount,refundsReason);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @param orderid 订单id
	* @param invoiceid 用户发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月27日 下午4:42:04     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "用户申请发票", notes = "用户申请发票")
	@RequestMapping(value = "/getInvoice", method = RequestMethod.POST)
	public AjaxJson getInvoice(String orderid,String invoiceid,String addressid){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.getInvoice(orderid, invoiceid,addressid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "用户发起支付", notes = "用户发起支付")
	@RequestMapping(value = "/initiatePay", method = RequestMethod.POST)
	public AjaxJson initiatePay(String orderid,String channel,String backUrl){
		Map map=new HashMap();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.initiatePay(orderid, channel,backUrl);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 查询用户发生退款的订单 
	* @param userid 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月24日 下午3:58:45     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询用户发生退款的订单", notes = "查询用户发生退款的订单")
	@RequestMapping(value = "/findRefundsOrders", method = RequestMethod.POST)
	public AjaxJson findRefundsOrders(String userid){
		List<ProvideOrderFormForOrder> orders=new ArrayList<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(orders);
		}else{
			try {
				orders=personalOrderFormService.findRefundsOrders(userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(orders);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(orders);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 查询用户有效的优惠券 
	* @param userid 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午4:01:42     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询用户有效的优惠券", notes = "查询用户有效的优惠券")
	@RequestMapping(value = "/findCoupons", method = RequestMethod.POST)
	public AjaxJson findCoupons(String userid){
		List<Coupons> coupons=new ArrayList<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(coupons);
		}else{
			try {
				coupons=personalOrderFormService.findCoupons(userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(coupons);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(coupons);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}


	
	/**
	 * 
	* @Description: 验证是否符合使用优惠券，如果符合，将优惠券及订单信息进行更新 
	* @param orderid 订单id
	* @param couponid 优惠券id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午9:17:59     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "验证优惠券", notes = "验证优惠券")
	@RequestMapping(value = "/orderToCoupon", method = RequestMethod.POST)
	public AjaxJson orderToCoupon(String orderid,String couponid){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.orderToCoupon(orderid, couponid); 
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	
	/**
	 * 
	* @Description: 分页查询用户有效的优惠券 
	* @param userid 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午4:01:42     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "分页查询用户有效的优惠券", notes = "分页查询用户有效的优惠券")
	@RequestMapping(value = "/findCouponsByPage", method = RequestMethod.POST)
	public AjaxJson findCouponsByPage(String userid, Integer pageNumber, Integer pageSize,Integer orderBy){
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findCouponsByPage(userid,pageNumber,pageSize,orderBy);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}

	/* *
	 * @Description:分页查询用户已经使用的优惠券
	 * @author JTing
	 * @date 2017/11/22 14:19
	 * @param [userid, pageNumber, pageSize ,orderBy]
	 * @return guoanmaker.personal.helper.common.AjaxJson
	 */
	@ApiOperation(value = "分页查询用户已经使用的优惠券",notes = "分页查询用户已经使用的优惠券")
	@PostMapping(value = "/findUsedCouponsByPage")
	public AjaxJson findUsedCouponsByPage(String userid,Integer pageNumber, Integer pageSize,Integer orderBy) {
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379);
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findUsedCouponsByPage(userid,pageNumber,pageSize,orderBy);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}

	/* *
	 * @Description:根据orderid分页查询可用的优惠卷
	 * @author JTing
	 * @date 2017/11/22 16:07
	 * @param [orderid,pageNumber,pageSize,orderBy]
	 * @return guoanmaker.personal.helper.common.AjaxJson
	 */
	@ApiOperation(value = "根据orderid分页查询可用的优惠卷",notes = "根据orderid分页查询可用的优惠卷")
	@PostMapping(value = "/findUsefulCouponsByPage")
	public AjaxJson findUsefulCouponsByPage(String orderid,Integer pageNumber, Integer pageSize,Integer orderBy) {
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379);
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findUsefulCouponsByPage(orderid,pageNumber,pageSize,orderBy);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;

	}

	/* *
	 * @Description:根据orderid分页查询不可用的优惠卷
	 * @author JTing
	 * @date 2017/11/22 16:07
	 * @param @param [orderid,pageNumber,pageSize,orderBy]
	 * @return guoanmaker.personal.helper.common.AjaxJson
	 */
	@ApiOperation(value = "根据orderid分页查询不可用的优惠卷",notes = "根据orderid分页查询不可用的优惠卷")
	@PostMapping(value = "/findNoUsefulCouponsByPage")
	public AjaxJson findNoUsefulCouponsByPage(String orderid,Integer pageNumber, Integer pageSize,Integer orderBy) {
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379);
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findNoUsefulCouponsByPage(orderid,pageNumber,pageSize,orderBy);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;

	}

	/* *
 * @Description:分页查询用户未使用已经过期的优惠卷
 * @author JTing
 * @date 2017/11/22 14:36
 * @param [userid, pageNumber, pageSize ,orderBy]
 * @return guoanmaker.personal.helper.common.AjaxJson
 */
	@ApiOperation(value = "分页查询用户未使用已经过期的优惠卷",notes = "分页查询用户未使用已经过期的优惠卷")
	@PostMapping(value = "/findExpiredCouponsByPage")
	public AjaxJson findExpiredCouponsByPage(String userid,Integer pageNumber, Integer pageSize ,Integer orderBy) {
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379);
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findExpiredCouponsByPage(userid,pageNumber,pageSize,orderBy);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;

	}
	/* *
	 * @Description:获得用户领取的双12优惠卷id
	 * @author JTing
	 * @date 2017/12/01 8:59
	 * @param [userid]
	 * @return guoanmaker.personal.helper.common.AjaxJson
	 */
	@ApiOperation(value = "获得用户领取的双12优惠卷id",notes = "获得用户领取的双12优惠卷id")
	@PostMapping(value = "/getUser1212Coupons")
	public AjaxJson checkUserisGet12Coupons(String userid) {
		Map map=new HashMap<>();
//		String sessionid=request.getSession().getId();
//		String sessionid1=request.getHeader("Authorization");
//		if(sessionid1!=null){
//			sessionid=sessionid1.substring(7,sessionid1.length());
//		}
//		Jedis jedis=new Jedis("123.56.48.63",6379);
//		jedis.auth("218007");
//		String value=jedis.get(sessionid);
//		jedis.disconnect();
//		if(value==null||!value.equals("1")){
//			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
//		}else{
			try {
				map=personalOrderFormService.getUser1212Coupons(userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
//		}
		return ajaxJson;

	}
	/**
	 * 
	* @Description: 查询用户全部优惠券，包括已过期和已使用
	* @param userid 用户id
	* @param pageNumber 分页页数，从0开始
	* @param pageSize 每页条数
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月14日 上午9:17:54     
	* @return Map    
	* @throws 
	 */
	@ApiOperation(value = "查询用户已过期和已使用的优惠券", notes = "查询用户已过期和已使用的优惠券")
	@RequestMapping(value = "/findAllCouponsByPage", method = RequestMethod.POST)
	public AjaxJson findAllCouponsByPage(String userid, Integer pageNumber, Integer pageSize){
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findAllCouponsByPage(userid,pageNumber,pageSize);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	/* *
	 * @Description:查询用户可用,已用,过期的优惠卷的数目
	 * @author JTing
	 * @date 2017/11/27 8:55
	 * @param [userid]
	 * @return guoanmaker.personal.helper.common.AjaxJson
	 */
	@ApiOperation(value = "查询用户可用,已用,过期的优惠卷的数目", notes = "查询用户可用,已用,过期的优惠卷的数目")
	@PostMapping(value = "/findAllTypeCouponsCountbyuserid")
	public AjaxJson findAllCouponsByPage(String userid){
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379);
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=personalOrderFormService.findAllTypeCouponsCountbyuserid(userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "初始化truePrice", notes = "初始化truePrice")
	@RequestMapping(value = "/initializeTruePrice", method = RequestMethod.POST)
	public AjaxJson initializeTruePrice(){
		try {
			personalOrderFormService.initializeTruePrice();
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData("初始化完成");
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("初始化失败");
		}
		return ajaxJson;
	}
	
	
	/**
	 * 
	* @Description: 创建一口价商品的秒杀订单 
	* @param userid 用户id
	* @param spikeid 秒杀id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param requirementDescription 需求描述
	* @param placeOrderType 下单渠道
	* @param serverPlace 服务区域
	* @param rangeid 
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月22日 下午4:16:15     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建一口价商品秒杀订单", notes = "创建一口价商品订单")
	@RequestMapping(value = "/createSpikeNotBargainingOrder", method = RequestMethod.POST)
	public AjaxJson createSpikeNotBargainingOrder(String userid,String spikeid,String contactPerson,String contactNumber,String requirementDescription,String serverPlace,String rangeid){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createSpikeNotBargainingOrder(userid, spikeid, contactPerson, contactNumber, requirementDescription,"1",serverPlace,rangeid);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建工位租用秒杀订单 
	* @param userid 用户id
	* @param spikeid 秒杀id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param beginDate 租用起始时间
	* @param endDate 租用结束时间
	* @param quantity 数量
	* @param @param requirementDescription
	* @param @param placeOrderType
	* @param @param serverPlace
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月22日 下午4:17:51     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "创建工位租用秒杀订单", notes = "创建工位租用秒杀订单")
	@RequestMapping(value = "/createSpikeSeatLeaseOrder", method = RequestMethod.POST)
	public AjaxJson createSpikeSeatLeaseOrder(String userid,String spikeid,String contactPerson,String contactNumber,String beginDate,String endDate,int quantity,String requirementDescription,String placeOrderType,String serverPlace){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalOrderFormService.createSpikeSeatLeaseOrder(userid, spikeid, contactPerson, contactNumber, beginDate,endDate, quantity, requirementDescription, "1",serverPlace);		
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	
	/**
	 * 
	* @Description: 用户领取优惠券接口 
	* @param userid 用户id
	* @param couponid 优惠券id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月29日 下午3:03:25     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "用户主动领取优惠券接口", notes = "用户主动领取优惠券接口")
	@RequestMapping(value = "/receiveUserCoupon", method = RequestMethod.POST)
	public AjaxJson receiveUserCoupon(String userid,String couponid){
		ReturnResult returnResult=new ReturnResult();
//		String sessionid=request.getSession().getId();
//		String sessionid1=request.getHeader("Authorization");
//		if(sessionid1!=null){
//			sessionid=sessionid1.substring(7,sessionid1.length());
//		}
//		Jedis jedis=new Jedis("123.56.48.63",6379);
//		jedis.auth("218007");
//		String value=jedis.get(sessionid);
//		jedis.disconnect();
//		if(value==null||!value.equals("1")){
//			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
//		}else{
			try {
				returnResult=personalUserCouponService.receiveUserCoupon(userid, couponid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
//		}
		return ajaxJson;
	}
	/**
	 * 
	 * @Title: getCurrentDate
	 * @Description: 获取当前时间
	 * @return AjaxJson 
	 * @throws 
	 */
	@ApiOperation(value = "获取系统时间", notes = "获取系统时间")
	@GetMapping("/getCurrentDate")
	public AjaxJson getCurrentDate(){
		return ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorTimeService.getCurrentDate());
	}
}
